/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.ElevatorAttachmentHistory;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.MaintainPlanContentExt;
import org.springblade.modules.admin.enums.ElevatorAttachmentEnum;
import org.springblade.modules.admin.service.IElevatorAttachmentHistoryService;
import org.springblade.modules.admin.service.IMaintainPlanContentExtService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.service.IRegionService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.PartAddress;
import org.springblade.modules.admin.vo.PartAddressVO;
import org.springblade.modules.admin.wrapper.PartAddressWrapper;
import org.springblade.modules.admin.service.IPartAddressService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 配件收货地址 控制器
 *
 * @author ysq
 * @since 2023-09-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/partaddress")
@Api(value = "配件收货地址", tags = "配件收货地址接口")
public class PartAddressController extends BladeController {

	private final IPartAddressService partAddressService;
	private final IRegionService regionService;
	private final IElevatorAttachmentHistoryService elevatorAttachmentHistoryService;
	private final IMaintainPlanContentService maintainPlanContentService;
	private final IMaintainPlanContentExtService planContentExtService;

	/**
	 * 详情
	 * 2025-06 新增返回工单ext
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入planId")
	public R<PartAddress> detail(Long planId) {
		if (planId == null || planId == 0) {
			PartAddress detail = partAddressService.getOne(Wrappers.<PartAddress>lambdaQuery().eq(PartAddress::getPlanId, 0).orderByDesc(PartAddress::getCreateTime).last("limit 1"));
			return R.data(detail);
		} else {
			PartAddress detail = partAddressService.getOne(Wrappers.<PartAddress>lambdaQuery().eq(PartAddress::getPlanId, planId));
			MaintainPlanContent planContent = maintainPlanContentService.getById(planId);
			List<ElevatorAttachmentHistory> historyList = elevatorAttachmentHistoryService.list(Wrappers.<ElevatorAttachmentHistory>lambdaQuery().eq(ElevatorAttachmentHistory::getElevatorId, planContent.getElevatorId()).eq(ElevatorAttachmentHistory::getStatus, ElevatorAttachmentEnum.NAME.getStatus()).orderByDesc(ElevatorAttachmentHistory::getCreateTime));
			if (Func.isNotEmpty(historyList)) {
				detail.setNameAttachment(historyList.get(0));
			}
			// 使用list，避免测试环境极端数据导致getOne出现问题
			List<MaintainPlanContentExt> extList = planContentExtService.list(
				Wrappers.<MaintainPlanContentExt>lambdaQuery()
					.eq(MaintainPlanContentExt::getPlanId, planId)
					.eq(BaseEntity::getIsDeleted, 0)
			);
			if(Func.isNotEmpty(extList)){
				detail.setPlanContentExt(extList.get(0));
			}
			return R.data(detail);
		}
	}

	/**
	 * 新增或修改 配件收货地址
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入partAddress")
	public R submit(@Valid @RequestBody PartAddress partAddress) {
		if (partAddress.getPlanId() == null) {
			partAddress.setPlanId(0L);
		}
		if (StrUtil.isNotEmpty(partAddress.getAdCode())) {
			List<Region> regions = regionService.list(Wrappers.<Region>lambdaQuery().eq(Region::getDistrictCode, partAddress.getAdCode()));
			if (regions.size() > 0) {
				partAddress.setProvinceName(regions.get(0).getProvinceName());
				partAddress.setCityName(regions.get(0).getCityName());
				partAddress.setAdName(regions.get(0).getDistrictName());
			}
		}
		return R.status(partAddressService.saveOrUpdate(partAddress));
	}


}
